<!--ID:sure-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link rel="stylesheet" href="../../libs/mui.min.css"/>
    <link rel="stylesheet" href="../../libs/app.css"/>
    <link rel="stylesheet" href="../../css/main.css"/>
    <link rel="stylesheet" href="../../css/sure.css"/>
    <script src="../../libs/mui.min.js"></script>
    <script src="../../js/jquery-3.1.1.min.js"></script>
    <script src="../../js/url.js"></script>
</head>

<body>
<div class="head">
    <img class="mui-action-back" src="../../images/all-left.png" alt="">
    <p>支付</p>
</div>
<div class="content">
    <!--<div class="content-d">
    <p>学校贷名校贷</p>
    <div>
        <img src="../../images/school.png" alt="">
    </div>
    <p>共<span></span>件，合计：<span class="red">¥<span></span></span></p>
</div>-->
</div>
<div class="way">
    <p>支付方式</p>
    <div class="way1">
        <span class="quan"></span>
        <img class="img1" src="../../images/vip-zhi.png" alt="">
        <span>支付宝</span>
    </div>
    <div class="way2">
        <span class="quan"></span>
        <img class="img2" src="../../images/vip-wei.png" alt="">
        <span>微信</span>
    </div>
</div>
<div class="accound">
    <div class="all-pay">
        <p>共<span class="how"></span>件，合计：<span class="red">¥<span class="allprice"></span></span>
        </p>
    </div>
    <div class="sure-pay">确认支付</div>
</div>
</body>
<script type="text/javascript">
    var ERROR_OK = 'S002';
    var singleOrderID; // 详情页单品购买ID

    $(function () {
        var data = JSON.parse(localStorage.getItem('details-sure'));
//        singleOrderID = plus.webview.currentWebview().singleOrderID;
        singleOrderID = data.singleOrderID ? data.singleOrderID : null;
//        localStorage.removeItem('details-sure'); // 在购物车购买的时候删除
        if (singleOrderID) {
            localStorage.carid = singleOrderID;
        }
    });

    $('.allprice').text(localStorage.sureallprice);
    $('.how').text(localStorage.surehow);

    /*选择微信还是支付宝支付*/
    $('.way1').on('tap', function () {
        $('.way1>.quan').addClass('addred');
        $('.way2>.quan').removeClass('addred');
    });
    $('.way2').on('tap', function () {
        $('.way2>.quan').addClass('addred');
        $('.way1>.quan').removeClass('addred');
    });

    /*点击[确认支付]判断是微信还是支付宝*/
    $('.sure-pay').on('tap', function () {
        if (!$('.way1>.quan').hasClass('addred') && !$('.way2>.quan').hasClass('addred')) {
            mui.toast('请选择支付方式');
        } else if ($('.way1>.quan').hasClass('addred')) {
            mui.alert('微信端暂不支持支付宝支付╮(╯▽╰)╭');
            console.log('支付宝');
//            pay('alipay');
        }
        if ($('.way2>.quan').hasClass('addred')) {
            console.log('微信');
//            pay('wxpay');
            weixinPay();
        }
    });

    function weixinPay() {
        $.ajax({ // 请求订单信息
            url: ur + 'rest/productController/wechat-pay-order',
            headers: {
                "X-Token": localStorage.token
            },
            type: 'POST',
            dataType: 'json',
            data: {
                openid: localStorage.trueOpenId,
                orderId: localStorage.carid,
                type: 'userWebParam'
            },
            success: function (res) {
                console.log('订单信息', res);
                if (res.meta.code == 'S003') { // code 若为 S003，则该笔订单无需支付，已经自动完成
                    mui.alert("支付成功！", function () {
                        href('../head-foot-demo.html#/helper')
                    });
                } else if (res.meta.code == ERROR_OK) {
                    pays_web(res.data);
                }
            }
        });
    }


    function pays_web(data) {
        function onBridgeReady() {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', data,
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                    } // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                }
            );
        }

        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady();
        }
    }

    function Clear() {
        localStorage.carNumber = 0;
    }
</script>

</html>